package com.campus.controller;

import com.campus.pojo.Echarts;
import com.campus.pojo.Notice;
import com.campus.pojo.Pager;
import com.campus.service.NoticeService;
import com.campus.tools.CommonTool;
import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    NoticeService service;
    JsonObject jsonObject;

    @RequestMapping("/getAllNotice")
    @ResponseBody
    public Map<String,Object> getAllNotice(HttpServletRequest request,
                                           int page, int rows){

        // 获取request中保存的当前登录用户的编号
        String userid = (String) request.getSession().getAttribute("USERID");
        Pager pager = new Pager();
        pager.setPerPageRows(rows);
        pager.setCurPage(page);
        Map<String,Object> params = new HashMap<>();
        params.put("pager",pager);
        params.put("userId",userid);

        int count = service .count(params);
        List<Notice> noticeList = service.selectByPage(params);

        Map<String,Object> result = new HashMap<>();
        result.put("total",count);
        result.put("rows",noticeList);
        return result;
    }

    @RequestMapping("/getRepairsNotice")
    @ResponseBody
    public Map<String,Object> getRepairsNotice(
            HttpServletRequest request, int page, int rows){

        // 获取request中保存的当前登录用户的编号
        String userid = (String) request.getSession().getAttribute("USERID");
        Pager pager = new Pager();
        pager.setPerPageRows(rows);
        pager.setCurPage(page);
        Map<String,Object> params = new HashMap<>();
        params.put("pager",pager);
        params.put("userId",userid);
        params.put("repair",1);//标记 维修信息
        int count = service .count(params);
        List<Notice> noticeList = service.selectByPage(params);

        Map<String,Object> result = new HashMap<>();
        result.put("total",count);
        result.put("rows",noticeList);
        return result;
    }



    // 根据id获取详细信息
    @RequestMapping(value = "/getNoticeById", method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public Map<String, Object> getNoticeById(Notice notice, HttpServletRequest req, HttpServletResponse rep) {
        Map<String, Object> result = new HashMap<String, Object>(2);
        List<Notice> list = service.getNoticeById(notice.getId());
        //System.out.println(list);//调试
        if (list.size() > 0) {
            result.put("success", true);
        } else {
            result.put("success", false);
        }
        result.put("rows", list);
        return result;
    }

    // 添加notice
    @RequestMapping(value = "/addNotice", method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public Map<String, Object> addNotice(Integer type, String secondIds, String content, String title, HttpServletRequest req) throws IOException {
        content = CommonTool.changeImageSrc(req, URLDecoder.decode(content, "utf-8"));
        Notice notice = new Notice();
        notice.setType(type);
        notice.setTitle(title);
        notice.setContent(content);
        String uid = (String) req.getSession().getAttribute("USERID");
        boolean flag = service.addNotice(notice, secondIds, uid);
        Map<String, Object> result = new HashMap<>();
        result.put("success", flag);
        return result;
    }

    // 删除
    @RequestMapping(value = "/delNotice", method = { RequestMethod.GET, RequestMethod.POST })
    public String delNotice(String ids, HttpServletRequest req, HttpServletResponse res) {
        boolean result = service.delNotice(ids);
        JsonObject jObject = new JsonObject();
        jObject.addProperty("success", result);
        try {
            ServletOutputStream jos = res.getOutputStream();
            jos.write(jObject.toString().getBytes(StandardCharsets.UTF_8));
            jos.flush();
            jos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 根据id获取信息详情
    @RequestMapping(value = "/getNoticeDetailNum", method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public Map<String, Object> getNoticeDetailNum(String nid, HttpServletRequest req, HttpServletResponse res) {
        Map<String, Object> result = new HashMap<String, Object>(2);
        List<Echarts> elr = service.getNoticeDetailNum(nid);
        List<Echarts> elv = service.getNoticeVoteNum(nid);
        result.put("success", true);
        result.put("read", elr);
        result.put("vote", elv);
        return result;
    }
}
